﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using CustomsNuke.Scheduling.Jobs;
using Quartz;
using System.Collections.ObjectModel;

namespace CustomsNuke.Scheduling.Utility
{
    public class CustomsAnalysUtils
    {
        public static LadingResult GetLadingInfo(string containerNo, string shipName, string shipNo)
        {
            LadingResult result = new LadingResult();

            if (string.IsNullOrEmpty(shipName) || string.IsNullOrEmpty(shipNo))
            {
                string htmlContent = HtmlParseUtils.ParsePage(string.Format("http://edi.easipass.com/dataportal/query.do?ctno={0}&blno=&pagesize=30&submit=%E6%9F%A5%E8%AF%A2&qn=dp_cst_vsl", containerNo));
                if (!string.IsNullOrEmpty(htmlContent))
                {
                    string content = HtmlParseUtils.FormatHtml(htmlContent, false, true);
                    if (!string.IsNullOrEmpty(content))
                    {
                        List<string> trList = HtmlParseUtils.GetSubStrings(content, "<tr height=\"21\">", null, "</tr>", "<tr height=\"21\">", "", "</tr>");
                        if (trList != null && trList.Count > 0)
                        {
                            if (trList[0].Contains("<a"))
                            {
                                result.IsSuccess = false;
                                result.IsNeedMoreInfo = true;
                                string errorMessage = string.Empty;
                                foreach (string s in trList)
                                {
                                    string tempShipName = HtmlParseUtils.GetSubStrings(s, "<td align=\"left\" bgcolor=\"#[A-Za-z0-9]+\">", null, "</td>", "<td align=\"left\" bgcolor=\"#EEEEEE\">", "<td align=\"left\" bgcolor=\"#E1E1E1\">", "</td>")[0];
                                    tempShipName = tempShipName.Remove(0, tempShipName.IndexOf('>') + 1);
                                    tempShipName = tempShipName.Substring(0, tempShipName.IndexOf("</a>"));

                                    string tempShipNo = HtmlParseUtils.GetSubStrings(s, "<td align=\"left\" bgcolor=\"#[A-Za-z0-9]+\">", null, "</td>", "<td align=\"left\" bgcolor=\"#EEEEEE\">", "<td align=\"left\" bgcolor=\"#E1E1E1\">", "</td>")[1];
                                    errorMessage += errorMessage + tempShipName + "," + tempShipNo + ";";
                                }
                                result.ErrorMessage = errorMessage;
                            }
                            else
                            {
                                result.IsSuccess = true;
                                List<string> headList = HtmlParseUtils.GetSubStrings(content, "<td align=\"left\">", null, "</td>", "<td align=\"left\">", "<td align=\"left\" width=\"200\">", "</td>");
                                if (headList != null && headList.Count > 2)
                                {
                                    result.Dock = headList[1].Remove(0, 9).Remove(4, 3);
                                }
                                else
                                {
                                    result.IsSuccess = false;
                                }
                                headList = HtmlParseUtils.GetSubStrings(content, "<td align=\"left\" width=\"200\">", null, "</td>", "<td align=\"left\">", "<td align=\"left\" width=\"200\">", "</td>");
                                if (headList != null && headList.Count > 2)
                                {
                                    result.ShipName = headList[0].Remove(0, 4).Trim();
                                    result.ShipNo = headList[1].Remove(0, 4).Trim();
                                }
                                else
                                {
                                    result.IsSuccess = false;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                string strUrlTemp = string.Format("http://edi.easipass.com/dataportal/query.do?qn=dp_cst_query_ctndetail&vslname={1}&voyage={2}&ctno={0}", containerNo, shipName, shipNo);

                string htmlContent = HtmlParseUtils.ParsePage(strUrlTemp);
                if (!string.IsNullOrEmpty(htmlContent))
                {
                    string content = HtmlParseUtils.FormatHtml(htmlContent, false, true);
                    if (!string.IsNullOrEmpty(content))
                    {
                        List<string> headList = HtmlParseUtils.GetSubStrings(content, "<td align=\"left\">", null, "</td>", "<td align=\"left\">", "<td align=\"left\" width=\"200\">", "</td>");
                        result.IsSuccess = true;
                        if (headList != null && headList.Count > 2)
                        {
                            result.Dock = headList[1].Remove(0, 9).Remove(4, 3);
                        }
                        else
                        {
                            result.IsSuccess = false;
                        }
                        headList = HtmlParseUtils.GetSubStrings(content, "<td align=\"left\" width=\"200\">", null, "</td>", "<td align=\"left\">", "<td align=\"left\" width=\"200\">", "</td>");
                        if (headList != null && headList.Count > 2)
                        {
                            result.ShipName = headList[0].Remove(0, 4).Trim();
                            result.ShipNo = headList[1].Remove(0, 4).Trim();
                        }
                        else
                        {
                            result.IsSuccess = false;
                        }
                    }
                }
            }

            return result;
        }

        public static CodecoResult GetCodecoInfo(string containerNo)
        {
            List<CodecoResult> resultList = new List<CodecoResult>();
            string codecoInfoRootHtmlContent = HtmlParseUtils.ParsePage("http://edi.easipass.com/dataportal/q.do?qn=dp_codecoinfo_query");
            if (!string.IsNullOrEmpty(codecoInfoRootHtmlContent))
            {
                string qid = HtmlParseUtils.GetSubString(codecoInfoRootHtmlContent, "<input name=\"qid\" value=\"", null,
                                                         "\" type=\"hidden\" />", null, null, null);
                if (!string.IsNullOrEmpty(qid))
                {
                    string codecoInfoHtmlContent = HtmlParseUtils.ParsePage(string.Format("http://edi.easipass.com/dataportal/query.do?ctn_no={0}&qid={1}&pagesize=30&submit=查询", containerNo, qid));
                    if (!string.IsNullOrEmpty(codecoInfoHtmlContent))
                    {
                        string content = HtmlParseUtils.FormatHtml(codecoInfoHtmlContent, false, true);
                        if (!string.IsNullOrEmpty(content))
                        {
                            List<string> trList = HtmlParseUtils.GetSubStrings(content, "<tr height=\"21\">", null, "</tr>", null, null, null);

                            if (trList != null)
                            {
                                foreach (string trItem in trList)
                                {
                                    List<string> tdList = HtmlParseUtils.GetSubStrings(trItem,"<td align=\"left\" bgcolor=\"#[A-Za-z0-9]+\">",null, "</td>","<td align=\"left\" bgcolor=\"#EEEEEE\">","<td align=\"left\" bgcolor=\"#E1E1E1\">","</td>");
                                    if (tdList.Count > 7)
                                    {
                                        if (tdList[6] == "出口")
                                        {
                                            CodecoResult result = new CodecoResult();
                                            result.ShipName = tdList[4];
                                            result.ShipNo = tdList[5];
                                            result.CodecoDate = tdList[10];
                                            result.IsSuccess = true;

                                            resultList.Add(result);
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return resultList.OrderByDescending(x=>x.CodecoDate).FirstOrDefault();
        }

        public static List<AdmissionResult> GetAdmissionInfo(string containerNo, string shipName, string shipNo)
        {
            List<AdmissionResult> resultList = new List<AdmissionResult>();

            string letPassRootHtmlContent = HtmlParseUtils.ParsePage("http://edi.easipass.com/dataportal/q.do?qn=dp_query_letpas");
            if (!string.IsNullOrEmpty(letPassRootHtmlContent))
            {
                string qid = HtmlParseUtils.GetSubString(letPassRootHtmlContent, "<input name=\"qid\" value=\"", null,
                                                         "\" type=\"hidden\" />", null, null, null);
                if (!string.IsNullOrEmpty(qid))
                {
                    string letPassHtmlContent = HtmlParseUtils.ParsePage(string.Format("http://edi.easipass.com/dataportal/query.do?entryid=&blno=&ctnno={0}&pagesize=30&submit=执行&qid={1}", containerNo, qid));
                    if (!string.IsNullOrEmpty(letPassHtmlContent))
                    {
                        string content = HtmlParseUtils.FormatHtml(letPassHtmlContent, false, true);
                        if (!string.IsNullOrEmpty(content))
                        {
                            List<string> trList = HtmlParseUtils.GetSubStrings(content, "<tr height=\"21\">", null, "</tr>", null, null, null);

                            if (trList != null)
                            {
                                foreach (string trItem in trList)
                                {
                                    List<string> tdList = HtmlParseUtils.GetSubStrings(trItem,"<td align=\"left\" bgcolor=\"#[A-Za-z0-9]+\">",null, "</td>","<td align=\"left\" bgcolor=\"#EEEEEE\">","<td align=\"left\" bgcolor=\"#E1E1E1\">","</td>");
                                    if (tdList.Count == 10)
                                    {
                                        AdmissionResult admissionInfo = new AdmissionResult();
                                        admissionInfo.ShipName = tdList[0];
                                        admissionInfo.ShipNo = tdList[1];
                                        admissionInfo.BillNo = tdList[2];
                                        admissionInfo.TrankNo = tdList[3];
                                        admissionInfo.DeclNo = tdList[4];
                                        admissionInfo.Dock = tdList[5];
                                        admissionInfo.AdmissionStatus = tdList[6];
                                        admissionInfo.EdiRecievedTime = tdList[7];
                                        admissionInfo.DockFeedback = tdList[8];
                                        admissionInfo.No = tdList[9];
                                        resultList.Add(admissionInfo);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(shipName) && !string.IsNullOrEmpty(shipNo))
            {
                resultList = resultList.FindAll(x => x.ShipName == shipName && x.ShipNo == shipNo);
            }

            return resultList;
        }

        public static OnboardingResult GetOnboardingInfo(string containerNo, string dock)
        {
            OnboardingResult result = new OnboardingResult();
            string strUrl = string.Empty;
            string content = string.Empty;
            List<string> trList = null;
            switch (dock)
            {
                case "洋山三期":
                    
                    strUrl = string.Format("http://query.sgict.com.cn/query/search/comm/contgoods.jsp?cntrno={0}&choice=1&Submit2.x=30&Submit2.y=11&Submit2=%CC%E1%BD%BB", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);
                    content = HtmlParseUtils.FormatHtml(content, false, true);
                    //TODO 可能会有问题，不能只查tr1
                    trList = HtmlParseUtils.GetSubStrings(content, "<tr class=\"result_tr[1-2]\" onClick", null, "/tr>", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td align=\"center\" height=\"25\">",
                                                                                null, "</td>",
                                                                                "<td align=\"center\" height=\"25\">",
                                                                                null, "</td>");
                            if (tdList != null && tdList.Count >= 7)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[1], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[6];
                                infoList.Add(info);
                            }
                        }
                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();

                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }

                    break;
                case "洋山一期":
                    strUrl = string.Format("http://www.shsict.com/as/query/search/comm/contgoods.jsp?cntrno={0}&choice=1&Submit2.x=20&Submit2.y=5&Submit2=%CC%E1%BD%BB", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);
                    content = HtmlParseUtils.FormatHtml(content, false, true);
                    //TODO 可能会有问题，不能只查tr1
                    trList = HtmlParseUtils.GetSubStrings(content, "<tr bgcolor=\"#E6EDFD\">", null, "</tr>", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td align=\"center\" height=\"35\">",
                                                                                null, "</td>",
                                                                                "<td align=\"center\" height=\"35\">",
                                                                                null, "</td>");
                            if (tdList != null && tdList.Count >= 7)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[1], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[6];
                                infoList.Add(info);
                            }
                        }

                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();

                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }
                    break;
                case "外一期":
                    strUrl = "http://www.spict.com/portinfo/Normal/CTNGInfosPage.aspx";
                    var headers = new WebHeaderCollection();
                    headers.Add("Referer", "http://www.spict.com/portinfo/Normal/CTNGInfosPage.aspx");
                    headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.31 (KHTML, like Gecko) Chrome/12.0.745.0 Safari/534.31");
                    headers.Add("Origin", "http://www.spict.com");
                    headers.Add("Content-Type", "application/x-www-form-urlencoded");

                    var postDataTemplte = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE0MTMwOTU5MDkPZBYCZg9kFgICAw9kFgICAQ88KwAHAQAPFgIeDl8hVXNlVmlld1N0YXRlZ2QWAmYPZBYEZg9kFgJmD2QWAmYPZBYCAgEPZBYKAgEPFgIeA2FsdAUt5LiK5rW35rWm5Lic5Zu96ZmF6ZuG6KOF566x56CB5aS05pyJ6ZmQ5YWs5Y%2B4ZAIDDzwrAAQBAA8WAh8AZ2RkAgUPFCsABA8WAh8AZ2RkZDwrAAQBABYCHgRUZXh0BQzlhazkvJfnlKjmiLdkAgcPFCsABA8WBB4FVmFsdWUFPn4vVXNlckxvZ2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3A100%2C%22st%22%3A%22%25%22%2C%22c%22%3Afalse%2C%22i%22%3A%5B%7B%22s%22%3A218%2C%22st%22%3A%22px%22%2C%22c%22%3Afalse%7D%2C%7B%22s%22%3A100%2C%22st%22%3A%22%25%22%2C%22c%22%3Afalse%2C%22i%22%3A%5B%7B%22s%22%3A100%2C%22st%22%3A%22%25%22%2C%22c%22%3Afalse%7D%5D%7D%5D%7D%5D%7D&ctl00_ASPxSplitter2_ASPxNavBar1GS=1%3B1&ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPagesATI=0&ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPages_txtCTNNO_Raw={0}&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24SearchPages%24txtCTNNO={0}&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24SearchPages%24txtCTNNO%24CVS=&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24SearchPages%24btnCTNNOSearch=&ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPages_txtBLNO_Raw=&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24SearchPages%24txtBLNO=%E6%8F%90%E5%8D%95%E5%8F%B7%E5%BF%85%E9%A1%BB%E8%BE%93%E5%85%A5&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24SearchPages%24txtBLNO%24CVS=&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24gridView%24DXSelInput=&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24gridView%24DXFocusedRowInput=-1&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24gridView%24CallbackState=%2FwEWBB4ERGF0YQXwBUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFGd0FBQUFWRFZFNU9Ud2JucnJIbGo3Y0hBQUFIUTFST1VsUkpUZ3pvdjV2bG5Mcm1sN2JwbDdRSUFBQUlRMVJPVWxSUFZWUU01WWU2NVp5NjVwZTI2WmUwQ0FBQUJrTlVUazFKVGcwZzZMK2I1Wnk2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FBQUFBQUFCZVFBY0RCd0VDQUFBQUFBQUFYa0FIQkFjQkFnQUFBQUFBQUY1QUJ3VUhBUUlBQUFBQUFBQmVRQWNHQndFQ0FBQUFBQUFBWGtBSEJ3Y0JBZ0FBQUFBQUFGNUFCd2dIQVFJQUFBQUFBQUJlUUFjSkJ3RUNBQUFBQUFBQVhrQUhDZ2NCQWdBQUFBQUFBRjVBQndzSEFRSUFBQUFBQUFCZVFBY01Cd0VDQUFBQUFBQUFYa0FIRFFjQkFnQUFBQUFBQUY1QUJ3OEhBUUlBQUFBQUFBQmVRQWNRQndFQ0FBQUFBQUFBWGtBSEVRY0JBZ0FBQUFBQUFGNUFCeElIQVFJQUFBQUFBQUJlUUFjVEJ3RUNBQUFBQUFBQVNVQUhGQWNCQWdBQUFBQUFBQUFBQndBSEFBY0FBZ0VHLy84SkFndERiMngxYlc1SmJtUmxlQWtDQUFJQUF3Y0FBZ0VIQUFJQkJ3QT0%3D&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24gridView%24DXColResizedInput=&ctl00%24ASPxSplitter2%24ContentPlaceHolder1%24gridView%24DXSyncInput=&ctl00_ASPxSplitter2_ContentPlaceHolder1_popupInfoWS=0%3A0%3A-1%3A-10000%3A-10000%3A0%3A600px%3A600px&DXScript=1_23%2C1_49%2C1_36%2C1_50%2C2_21%2C2_28%2C2_14%2C1_30%2C1_42%2C3_7%2C3_6%2C2_24%2C2_26%2C1_27%2C1_44%2C1_41";

                    var postData = Encoding.UTF8.GetBytes(string.Format(postDataTemplte, containerNo));

                    content = HtmlParseUtils.ParsePage(strUrl, postData, headers);

                    List<Tuple<int, DateTime>> resultList = new List<Tuple<int, DateTime>>();

                    var trs = HtmlParseUtils.GetSubStrings(content, "<tr[^>]+ctl00_ASPxSplitter2_ContentPlaceHolder1_gridView_DXDataRow[\\d].*?</tr>", string.Empty);
                    if (trs != null)
                    {
                        trs.ForEach(tr =>
                        {
                            var ctno = 0;
                            Int32.TryParse(HtmlParseUtils.GetSubStrings(tr, "ShowPopup[(]'\\d", null, "[)]", "ShowPopup('", "')", null).FirstOrDefault(), out ctno);
                            var inDate = DateTime.MinValue;
                            DateTime.TryParse(HtmlParseUtils.GetSubStrings(tr, "<td.*?</td>", "<[^>]+>")[1], out inDate);
                            resultList.Add(Tuple.Create<int, DateTime>(ctno, inDate));
                        });
                    }

                    var containerNumber = resultList.OrderByDescending(p => p.Item2).FirstOrDefault().Item1;
                    strUrl = string.Format("http://www.spict.com/portinfo/Normal/CTNInfoPage.aspx?ctnno={0}",containerNumber);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);

                    var inputElement = HtmlParseUtils.GetSubStrings(content, "<input[^>]+ctl00_ContentPlaceHolder1_tbxCTNRTIN_I", null, "/>", null, null, null);
                    var inDate1 = DateTime.MinValue;
                    DateTime.TryParse(HtmlParseUtils.GetSubStrings(inputElement[0], "value=\"", null, "\"", "value=\"", "\"", null).FirstOrDefault(), out inDate1);

                    inputElement = HtmlParseUtils.GetSubStrings(content, "<input[^>]+ctl00_ContentPlaceHolder1_tbxIsDS_I", null, "/>", null, null, null);
                    var status = HtmlParseUtils.GetSubStrings(inputElement[0], "value=\"", null, "\"", "value=\"", "\"", null).FirstOrDefault();

                    if (status == "Y")
                    {
                        result.IsOnboarding = true;
                        result.IsSuccess = true;
                    }

                    break;
                case "外二期":
                    strUrl = string.Format("http://211.144.118.73/search/comm/contgoods.jsp?cntrno={0}&choice=1&Submit2.x=20&Submit2.y=5&Submit2=%CC%E1%BD%BB", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);
                    content = HtmlParseUtils.FormatHtml(content, false, true);

                    trList = HtmlParseUtils.GetSubStrings(content, "<tr class=\"result_tr[1-2]\">", null, "</tr>", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td align=\"center\" height=\"25\">",
                                                   null, "</td>",
                                                   "<td align=\"center\" height=\"25\">",
                                                   null, "</td>");

                            if (tdList != null && tdList.Count >= 8)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[1], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[6];
                                infoList.Add(info);
                            }
                        }
                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();

                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }
                    break;
                case "外四期":
                    strUrl = string.Format("http://www.sect.com.cn/wat/controllerServlet.do?queryid=0003001&querysid=g0003&method=doquery&iyc_cntrno={0}", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.UTF8);
                    content = HtmlParseUtils.FormatHtml(content, false, true);

                    trList = HtmlParseUtils.GetSubStrings(content, "<tr><td>" + containerNo, null, "<a href=\"#\">", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td>",
                                                   null, "</td>",
                                                   "<td>",
                                                   null, "</td>");

                            if (tdList != null && tdList.Count == 7)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[1], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[4];
                                infoList.Add(info);
                            }
                        }
                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();

                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }
                    //TODO一定不是Y，上港和外四一样，没有配船

                    break;
                case "外五期":
                    strUrl = string.Format("http://www.smct.com.cn:81/search/comm/contgoods.jsp?cntrno={0}&choice=1&Submit4=%B2%E9++%D1%AF", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);
                    content = HtmlParseUtils.FormatHtml(content, false, true);

                    trList = HtmlParseUtils.GetSubStrings(content, "<tr class=\"result_tr[1-2]\" onClick", null, "</tr>", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td class=\"x2\" align=\"center\" height=\"25\">",
                                                   null, "</td>",
                                                   "<td class=\"x2\" align=\"center\" height=\"25\">",
                                                   null, "</td>");

                            if (tdList != null && tdList.Count == 7)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[1], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[6];
                                infoList.Add(info);
                            }
                        }
                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();

                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }
                    break;
                    
                default:
                    strUrl = string.Format("http://www.sctport.com.cn/newsctsite/sct/rescotrno.jsp?cntrno={0}&choice=1&Submit1.x=12&Submit1.y=10", containerNo);
                    content = HtmlParseUtils.ParsePage(strUrl, Encoding.Default);
                    content = HtmlParseUtils.FormatHtml(content, false, true);

                    trList = HtmlParseUtils.GetSubStrings(content, "<tr onMouseOver", null, "</tr>", "", null, null);
                    if (trList != null && trList.Count > 0)
                    {
                        List<OnboardingInfo> infoList = new List<OnboardingInfo>();
                        foreach (var c in trList)
                        {
                            List<string> tdList = HtmlParseUtils.GetSubStrings(c, "<td><div align=\"center\"><font size=\"2\">",
                                                   null, "</font></div></td>",
                                                   "<td><div align=\"center\"><font size=\"2\">",
                                                   null, "</font></div></td>");

                            if (tdList != null && tdList.Count == 7)
                            {
                                DateTime date = DateTime.MinValue;
                                DateTime.TryParse(tdList[2], out date);
                                OnboardingInfo info = new OnboardingInfo();
                                info.InDate = date;
                                info.OnboardingStatus = tdList[5];
                                infoList.Add(info);
                            }
                        }
                        OnboardingInfo realInfo = infoList.OrderByDescending(x => x.InDate).FirstOrDefault();
                        //TODO 一定不是Y，上港和外四一样，没有配船
                        if (realInfo.OnboardingStatus == "Y")
                        {
                            result.IsOnboarding = true;
                            result.IsSuccess = true;
                        }
                    }
                    break;
                    
            } 

            return result;
        }
    }

    public class LadingResult
    {
        public bool IsSuccess
        { 
            get; set;
        }
        public bool IsNeedMoreInfo
        { 
            get; set;
        }
        public string Dock
        { get; set;
        }
        public string BillNo
        { get; set;
        }
        public string ShipName
        { get; set;
        }
        public string ShipNo
        { get; set;
        }
        public string ErrorMessage
        {
            get; set;
        }
    }

    public class CodecoResult
    {
        public bool IsSuccess
        { get; set;
        }

        public string ShipName
        { get; set;
        }

        public string ShipNo
        { get; set;
        }

        public string CodecoDate
        { get; set;
        }
    }

    public class AdmissionResult
    {
        public bool IsSuccess
        { get; set;
        }

        public string ShipName
        {
            get;
            set;
        }
        public string ShipNo
        {
            get;
            set;
        }
        public string BillNo
        {
            get;
            set;
        }
        public string TrankNo
        {
            get;
            set;
        }
        public string DeclNo
        {
            get;
            set;
        }
        public string Dock
        {
            get;
            set;
        }
        public string AdmissionStatus
        {
            get;
            set;
        }
        public string EdiRecievedTime
        {
            get;
            set;
        }
        public string DockFeedback
        {
            get;
            set;
        }
        public string No
        {
            get;
            set;
        }
    }

    public class OnboardingResult
    {
        public bool IsSuccess
        {
            get; set;
        }
        public bool IsOnboarding
        {
            get; set;
        }
        public string EnteredDate
        {
            get;
            set;
        }
        public string LeftDate
        {
            get;
            set;
        }
        public string EnteredMode
        {
            get;
            set;
        }
        public string LeftMode
        {
            get;
            set;
        }
        public bool IsAdmitted
        {
            get;
            set;
        }
    }

    public class OnboardingInfo
    {
        public DateTime InDate
        {
            get;
            set;
        }

        public string OnboardingStatus
        {
            get;
            set;
        }
    }
}
